﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BZOrderPrint.DAL;

namespace BZOrderPrint
{
    public partial class FrmShop : Form
    {
        public int SelShopID { get; set; }
        public Dictionary<int, string> Shops { get; set; }

        public FrmShop()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            Shops = new Dictionary<int, string>();
            using (var db = new BZContext(ConfigHelper.ConnString))
            {
                var data = db.bzShopEntities.Where(x => new int[] { (int)AppEnum.ShopType.Directly, (int)AppEnum.ShopType.Join }.Contains(x.ShopType)).ToList();
                foreach (var shop in data)
                {
                    Shops.Add(shop.Id, shop.ShopName);
                }
            }

            lbShop.DataSource = Shops.Values.ToList();
        }

        private void btnYes_Click(object sender, EventArgs e)
        {
            if (lbShop.SelectedValue != null)
            {
                var item = Shops.FirstOrDefault(x => x.Value.Equals(lbShop.SelectedValue.ToString()));
                SelShopID = item.Key;
            }
            this.DialogResult = DialogResult.OK;
        }

        private void btnNo_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                lbShop.DataSource = Shops.Values.ToList();
            }
            else
            {
                lbShop.DataSource = Shops.Values.Where(x => x.Contains(textBox1.Text)).ToList();
            }
        }
    }
}